package com.axzhengxin.platform.business.service.impl;

import com.axzhengxin.platform.business.domain.BizApplyInfo;
import com.axzhengxin.platform.business.mapper.BizAllApplyMapper;
import com.axzhengxin.platform.business.service.IBizAllApplyService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @Author AHZJ0005
 * @Date 2021/2/26-10:16
 **/
@Slf4j
@Service
public class BizAllApplyServiceImpl implements IBizAllApplyService {
    @Autowired
    private BizAllApplyMapper bizAllApplyMapper;


    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<BizApplyInfo> list(BizApplyInfo bizApplyInfo) {
        List<BizApplyInfo> list = bizAllApplyMapper.list(bizApplyInfo);
        for (BizApplyInfo applyInfo : list) {
            Integer status = applyInfo.getStatus();
            String type = applyInfo.getBizType();
            if (status == null || status == 0) {
                applyInfo.setStatusTag("抢单中");
            } else if (status == 10 && "APPLY".equals(type)) {
                applyInfo.setStatusTag("待分配");
            } else if (status == 1010 && "APPLY".equals(type)) {
                applyInfo.setStatusTag("待审核");
            } else if (status == 1010 && "APPROVE".equals(type)) {
                applyInfo.setStatusTag("待授信");
            } else if (status == 1010 && "LOAN".equals(type)) {
                applyInfo.setStatusTag("待放款");
            } else if (status == 2000 && "LOAN".equals(type)) {
                applyInfo.setStatusTag("已完成");
            } else if (status == 3000) {
                applyInfo.setStatusTag("已拒绝");
            } else if (status == 4000) {
                applyInfo.setStatusTag("已撤销");
            }
        }

        list = buildBizApplyList(list);
        return list;
    }

    public List<BizApplyInfo> buildBizApplyList(List<BizApplyInfo> bizApplyInfos) {
        bizApplyInfos.stream().
                forEach(bizApplyInfo1 -> {
                    BigDecimal approveAmount = bizApplyInfo1.getApproveAmount();
                    if (approveAmount != null) {
                        bizApplyInfo1.setApproveAmount(approveAmount.setScale(2, RoundingMode.HALF_UP));
                    }
                    String pciBelongNo = bizApplyInfo1.getPciBelongNo();
                    if (StringUtils.isNotBlank(pciBelongNo)) {
                        String biz_industry_type = DictUtils.getDictLabel("biz_industry_type", pciBelongNo);
                        if (StringUtils.isNotBlank(biz_industry_type)) {
                            bizApplyInfo1.setPciBelongType(biz_industry_type);
                        }
                    }
                    String customerAreaNo = bizApplyInfo1.getCustomerAreaNo();
                    if (StringUtils.isNotBlank(customerAreaNo)) {
                        String area_codes = DictUtils.getDictLabel("area_codes", customerAreaNo);
                        if (StringUtils.isNotBlank(area_codes)) {
                            bizApplyInfo1.setCustomerArea(area_codes);
                        }
                    }
                });
        return bizApplyInfos;
    }
}
